package com.xunk.equipment.service.mandun.task;

import com.alibaba.fastjson.JSONObject;
import com.xunk.common.core.utils.StringUtils;
import com.xunk.common.redis.service.RedisService;
import com.xunk.equipment.config.MandunConfig;
import com.xunk.equipment.service.mandun.constant.MandunConstant;
import com.xunk.equipment.service.mandun.service.IMandunService;
import com.xunk.equipment.service.mandun.util.MandunUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.equipment.service.mandun.task
 * @ClassName: RefreshTokenTask
 * @Author: luanzhiwei
 * @Description: 曼顿定时刷新token任务
 * @Date: 2021/4/13 20:14
 * @Version: 1.0
 */
@Component
@Slf4j
public class RefreshTokenTask {


    @Autowired
    private MandunConfig mandunConfig;
    @Autowired
    private RedisService redisService;
    @Autowired
    private IMandunService mandunService;

//    @Scheduled(cron = "0 */5 * * * ?")
    public void refreshToken() {
        log.info("曼顿获取/刷新token开始");
        String accessToken = redisService.getCacheObject(MandunConstant.MANDUN_ACCESS_TOKEN);
        if (StringUtils.isNotEmpty(accessToken)) {
            log.info("accessToken未失效，不刷新token");
            return;
        }
        String refreshToken = redisService.getCacheObject(MandunConstant.MANDUN_REFRESH_TOKEN_KEY);
        if (StringUtils.isEmpty(refreshToken)) {
            log.info("refreshToken已失效，重新获取token");
            mandunService.accessToken(mandunConfig, StringUtils.EMPTY);
            return;
        }
        mandunService.refreshToken(mandunConfig, refreshToken);
        log.info("曼顿获取/刷新token结束");
    }

}
